
import random, time
from scripts.utils import input_reading, constants
from dexen.libs import dexen_api

""" GLOBALS """
script_api = dexen_api.getScriptApi()
boxes = input_reading.read_input()
n_boxes = len(boxes)

def generate_individual():
    individual = dict(state=constants.ALIVE,
                      gamma_x=[],
                      gamma_y=[],
                      orient=[],
                      encoding=range(n_boxes),
                      phenotype=None,
                      min_width=None,
                      min_height=None,
                      area=None)
    
    individual['gamma_x'] = range(n_boxes)
    random.shuffle(individual['gamma_x'])
    individual['gamma_y'] = range(n_boxes)
    random.shuffle(individual['gamma_y'])
    
    for i, box_no in enumerate(individual['gamma_x']):
        individual['encoding'][box_no] = dict(x=i)
    for i, box_no in enumerate(individual['gamma_y']):
        individual['encoding'][box_no]['y'] = i
    for i in range(n_boxes):
        individual['orient'].append(random.randint(0, 1))
    return individual  

def initialise():
    population = []
    cur_time = time.time()
    while len(population) < constants.INITIALIZATION_SIZE:
        individual = generate_individual()
        population.append(individual)
    
    #print "initialise", time.time() - cur_time, "seconds..."
    #print "I am sending the individuals to the server"
    script_api.uploadIndividuals(population)